package com.ssyu.examination.assist.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.assist.entity.AssitDbDateEntity;
import com.ssyu.examination.assist.service.AssitDbDateSevrice;
import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.MySQLDatabaseBackup;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;

@WebServlet("/AssitDbDateServlet")
public class AssitDbDateServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public AssitDbDateServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("listDatebase".equals(command)) {
			listDatebase(request, response);
		} else if ("addDatebase".equals(command)) {
			addDatebase(request, response);
		}
	}

	private void listDatebase(HttpServletRequest request,
			HttpServletResponse response) {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		AssitDbDateSevrice assit = new AssitDbDateSevrice();
		PageModel<AssitDbDateEntity> list = assit.listDateBase(page, rows);
		if (list != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(list,
					"yyyy-MM-dd HH:mm:ss"));
		}
	}

	private void addDatebase(HttpServletRequest request,
			HttpServletResponse response) {
		MySQLDatabaseBackup mySQLDatabaseBackup = new MySQLDatabaseBackup();
		mySQLDatabaseBackup.executeBackup();
		Date time = new Date();
		String bakUrl = "D:/backupDatabas/" + mySQLDatabaseBackup.getSQLName();
		Date bakTime = time;
		int operateUserId = App.getOperateId(request, response);

		List<Object> list = new ArrayList<>();
		list.add(bakUrl);
		list.add(bakTime);
		list.add(operateUserId);
		StringBuilder sb = new StringBuilder();
		removeDatebase(request, response);
		sb.append("INSERT INTO assit_db_data (`id`, `bak_url`, `bak_time`,`operate_user_id`) VALUES (NULL, ?, ?, ?) ");
		int row = DBUtil.executeUpdate(sb.toString(), list);
		if (row == 1) {
			print(response, 1);
		} else {
			print(response, null);
		}
	}

	private void removeDatebase(HttpServletRequest request,
			HttpServletResponse response) {
		String str = "SELECT COUNT(*) FROM assit_db_data ";
		int count = DBUtil.getCount(str);
		if (count >= 10) {
			int row = DBUtil
					.executeUpdate("DELETE FROM assit_db_data LIMIT 1;");
			if (row == 1) {
				print(response, 1);
			}
		} else {
			return;
		}
	}

}
